import qs from "qs";
import type { CommonResult, RequestOption } from "@/types/request";
import { ROUTER_PATH } from "@/concacts/routerPath";
import { getLocalStorage } from "./storage";

const baseServer = `https://api.it120.cc/xiaochengxu`;
const baiduServer = `https://fanyi-api.baidu.com/api/trans/vip/translate`;

// content-type 类型
export const ENUM_CONTENT_TYPE = {
  FORM: "application/x-www-form-urlencoded", //url编码格式
  JSON: "application/json", //json格式
};

// 封装请求方法
export async function request(options: RequestOption) {
  const {
    url,
    payload,
    method = "GET",
    showToast = true,
    contentType = "application/x-www-form-urlencoded",
    urlPrefix = "",
    isPublic = false,
  } = options;

  const token = await getLocalStorage("token");
  const requestUrl = `${urlPrefix}${baseServer}${url}`;

  const header: any = token ? { token: token } : {};
  if (method === "POST") {
    header["content-type"] = contentType;
  }
  if (method === "DELETE") {
    header["content-type"] = contentType;
  }

  console.log(contentType, "请求头类型");

  //请求拦截器
  uni.addInterceptor("request", {
    invoke(args) {
      console.log("🚀 ~ invoke ~ args:", args);
      if (
        method === "GET" ||
        (method === "POST" &&
          contentType === "application/x-www-form-urlencoded")
      ) {
        console.log("是否进入该分支");

        if (args?.data) {
          args.url =
            args.url + "?" + qs.stringify(args.data, { indices: false });
          args.data = undefined;
        }
      }
    },
  });

  return new Promise((resolve, reject) => {
    uni.request({
      url: requestUrl,
      method,
      data: payload,
      header,
      success: (res: any) => {
        // const { status } = res.data;
        // if (status === STATUS_ERROR) {
        //   const err = res.data;
        //   const defaultMsg = "请求异常";
        //   if (showToast) {
        //     uni.showToast({
        //       title: err?.description || defaultMsg,
        //       icon: "none",
        //     });
        //   }
        //   reject(res.data);
        // } else {
        //   resolve(res.data);
        // }
        resolve(res.data);
      },
      fail: (err: any) => {
        uni.showToast({
          title: "网络开小差了~请检查网络再重试",
          icon: "none",
        });
        reject(err);
      },
    });
  });
}


export async function baiduRequest(options: RequestOption):Promise<any> {
  const {
    url,
    payload,
    method = "GET",
    showToast = true,
    contentType = "application/x-www-form-urlencoded",
    urlPrefix = "",
    isPublic = false,
  } = options;

  const token = await getLocalStorage("token");
  const requestUrl = `${urlPrefix}${baiduServer}${url}`;

  const header: any = token ? { token: token } : {};
  if (method === "POST") {
    header["content-type"] = contentType;
  }
  if (method === "DELETE") {
    header["content-type"] = contentType;
  }

  console.log(contentType, "请求头类型");

  //请求拦截器
  uni.addInterceptor("request", {
    invoke(args) {
      console.log("🚀 ~ invoke ~ args:", args);
      if (
        method === "GET" ||
        (method === "POST" &&
          contentType === "application/x-www-form-urlencoded")
      ) {
        console.log("是否进入该分支");

        if (args?.data) {
          args.url =
            args.url + "?" + qs.stringify(args.data, { indices: false });
          args.data = undefined;
        }
      }
    },
  });

  return new Promise((resolve, reject) => {
    uni.request({
      url: requestUrl,
      method,
      data: payload,
      header,
      success: (res: any) => {
        // const { status } = res.data;
        // if (status === STATUS_ERROR) {
        //   const err = res.data;
        //   const defaultMsg = "请求异常";
        //   if (showToast) {
        //     uni.showToast({
        //       title: err?.description || defaultMsg,
        //       icon: "none",
        //     });
        //   }
        //   reject(res.data);
        // } else {
        //   resolve(res.data);
        // }
        resolve(res.data);
      },
      fail: (err: any) => {
        uni.showToast({
          title: "网络开小差了~请检查网络再重试",
          icon: "none",
        });
        reject(err);
      },
    });
  });
}